﻿Imports System.Collections.ObjectModel
Imports System.Data.Objects
Imports ASAP.TiposDatosSTE

Class MainWindow

    Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        Me.TextBox1.Text = "Z:\usr\Oriol\ASAP\Intermedio"
        Me.TextBox2.Text = "Z:\usr\Oriol\ASAP\Teoria"
    End Sub

#Region "Lecciones"

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click

        Try
            Dim total As Integer = System.IO.Directory.GetDirectories(Me.TextBox1.Text).Length
            Dim i As Integer = 0

            For Each pathCarpeta In System.IO.Directory.GetDirectories(Me.TextBox1.Text)
                Label3.Content = "Tratando: " & pathCarpeta
                TrataCarpeta(pathCarpeta)
                i = i + 1
                Label4.Content = "Tratadas " & i & " de " & total
            Next
            MsgBox("Fin")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    End Sub

    Private Sub TrataCarpeta(ByVal path As String)

        Dim files As ReadOnlyCollection(Of String)
        files = My.Computer.FileSystem.FindInFiles(path, Nothing, True, FileIO.SearchOption.SearchAllSubDirectories, "*.xml")

        For Each file As String In files
            Dim clsMigraLeccion As New MigraLeccion
            clsMigraLeccion.Procesa(file, True)
        Next

    End Sub

    Private Sub TrataCarpetaAnadeComentarios(ByVal path As String)

        Dim files As ReadOnlyCollection(Of String)
        files = My.Computer.FileSystem.FindInFiles(path, Nothing, True, FileIO.SearchOption.SearchAllSubDirectories, "*.xml")

        For Each file As String In files
            Dim ac As New AnadeComentario
            ac.Procesa(file)
        Next

    End Sub

#End Region

#Region "Teoria"

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button2.Click
        Try

            Dim files As ReadOnlyCollection(Of String)
            files = My.Computer.FileSystem.FindInFiles(TextBox2.Text, Nothing, True, FileIO.SearchOption.SearchAllSubDirectories, "*.docx")

            Dim t As Teoria
            Using c As New InterfazNegocio.TiposDatosSTE.ContenedorEntidades
                For Each file As String In files
                    t = New Teoria
                    Dim i As Integer = file.LastIndexOf("\") + 1
                    t.TituloTeoria = file.Substring(i, file.Length - i)
                    t.TituloTeoria = t.TituloTeoria.Replace(".docx", "")
                    t.Documento = My.Computer.FileSystem.ReadAllBytes(file)
                    c.Teorias.AddObject(t)
                Next
                c.SaveChanges()
            End Using

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

#End Region


End Class
